package com.zzl.java8.czbk.stream.test1;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * description: Stream API基础应用测试
 * @author 朱正磊
 * @date 2022-06-20 14:10:10
 */
public class StreamApiTest1 {

    /**
     * description: 基于Collection集合生成流
     * @author 朱正磊
     * @date 2022-06-20 14:11:32
     */
    @Test
    void test1() {
        List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("www");
        list.add("pbteach");
        list.add("com");
        // 调用stream()方法生成流对象
        Stream<String> stream = list.stream();
        // 对流进行遍历
        stream.forEach(System.out::println);
        // 简化写法
        list.stream().forEach(System.out::println);
        // 极简化写法
        list.forEach(System.out::println);
    }

    /**
     * description: 基于Map生成流对象
     * @author 朱正磊
     * @date 2022-06-20 14:11:37
     */
    @Test
    void test2() {
        Map<String, PbCourse> map = new HashMap<>();
        map.put("100", new PbCourse(100L, "攀博课堂-Java面向对象教程", 33));
        map.put("101", new PbCourse(101L, "攀博课堂-Spring全家桶教程", 44));
        map.put("102", new PbCourse(102L, "攀博课堂-SpringCloud微服务教程", 55));
        map.put("103", new PbCourse(103L, "攀博课堂-在线教育分布式项目教程", 66));
        map.put("104", new PbCourse(104L, "攀博课堂-电子商务项目教程", 77));

        // 基于map生成流对象，我们可以对keySet、values生成流对象
        Set<String> keySet = map.keySet();
        Stream<String> keyStream = keySet.stream();

        keyStream.forEach(key -> {
            System.out.println(key + "    " + map.get(key));
        });

        Collection<PbCourse> values = map.values();
        Stream<PbCourse> valueStream = values.stream();
        valueStream.forEach(System.out::println);
    }

    /**
     * description: 基于数组生成流对象
     * @author 朱正磊
     * @date 2022-06-20 14:11:49
     */
    @Test
    void test3() {
        String[] strs = new String[]{"hello", "www", "pbteach", "com"};
        Stream<String> stringStream = Stream.of(strs);
        stringStream.forEach(System.out::println);

        int[] ints = new int[]{1, 2, 3, 4, 5, 6};

        Stream<int[]> intArrayStream = Stream.of(ints);
        // IntStream intStream1 = intArrayStream.flatMapToInt(x -> Arrays.stream(x));
        // IntStream intStream1 = intArrayStream.flatMapToInt(Arrays::stream);
        // IntStream intStream1 = intArrayStream.flatMapToInt(x -> IntStream.of(x));
        IntStream intStream1 = intArrayStream.flatMapToInt(IntStream::of);
        intStream1.forEach(System.out::println);

        IntStream intStream2 = Arrays.stream(ints);
        intStream2.forEach(System.out::println);
    }

}
